package day11.homework.dao.Impl;

import day11.homework.bean.Info;
import day11.homework.dao.InfoDao;
import day11.homework.dao.InfoTypeDao;
import day11.homework.utils.JDBCUtil3;

import java.sql.Date;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class InfoDaoImpl implements InfoDao {

    @Override
    public void save(Info info) {
        String sql = "insert into info(id, title, content, info_type_id, author, createDate)values(?,?,?,?,?,?)";
        String sql2 = "insert into info_type(id)values(?)";

        Object[] strs = {info.getId(), info.getTitle(), info.getContent(), info.getInfo_type_id(), info.getAuthor(), info.getCreateDate()};
        Object[] strs2 = {info.getInfo_type_id()};
        try {
            InfoTypeDao infoType = new InfoTypeDaoImpl();
            if (infoType.getInfoTypeById(info.getInfo_type_id()) == null) JDBCUtil3.executeUpdate(sql2, strs2);
            //只管sql，参数传进来
            JDBCUtil3.executeUpdate(sql, strs);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil3.closeConn(JDBCUtil3.getCo(), JDBCUtil3.getS(), null);
        }
    }

    @Override
    public void update(Info info) {
        String sql = "update info set id = ?, title = ?, content = ?, author = ?, createDate = ? where id = ?";
//        String sql2 = "insert into info_type(id)values(?)";
        Object[] strs = {info.getId(), info.getTitle(), info.getContent(), info.getAuthor(), info.getCreateDate(), info.getId()};
//        Object[] strs2 = {info.getInfo_type_id()};
        try {
            JDBCUtil3.executeUpdate(sql, strs);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil3.closeConn(JDBCUtil3.getCo(), JDBCUtil3.getS(), null);
        }
    }

    @Override
    public void delete(Info info) {
        String sql = "delete from info where id = ?";
        try {
            Object[] strs = {info.getId()};
            JDBCUtil3.executeUpdate(sql, strs);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil3.closeConn(JDBCUtil3.getCo(), JDBCUtil3.getS(), null);
        }
    }

    @Override
    public Info getInfoById(int id) {
        String sql = "select * from info where id = ?";
        ResultSet rs = null;
        try {
            Object[] strs = {id};
            rs = JDBCUtil3.executeQuery(sql, strs);
            // 返回结果集 遍历
            while (rs.next()) {
                // 创建用户对象
                Info info = new Info();
                int uid = rs.getInt(1);
                String title = rs.getString("title");
                String content = rs.getString("content");
                int info_type_id = rs.getInt("info_type_id");
                String author = rs.getString("author");
                Date createDate = rs.getDate("createDate");

                info.setId(uid);
                info.setTitle(title);
                info.setContent(content);
                info.setInfo_type_id(info_type_id);
                info.setAuthor(author);
                info.setCreateDate(createDate);


                // 返回用户信息
                return info;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil3.closeConn(JDBCUtil3.getCo(), JDBCUtil3.getS(), rs);
        }
        return null;
    }

    @Override
    public List<Info> findInfos() {
        // 定义集合
        List<Info> infos = new ArrayList<>();
        ResultSet rs = null;
        try {
            String sql = "select * from info";
            rs = JDBCUtil3.executeQuery(sql, null);
            // 返回结果集 遍历
            while (rs.next()) {
                // 创建用户对象
                Info info = new Info();
                int uid = rs.getInt(1);
                String title = rs.getString("title");
                String content = rs.getString("content");
                int info_type_id = rs.getInt("info_type_id");
                String author = rs.getString("author");
                Date createDate = rs.getDate("createDate");

                info.setId(uid);
                info.setTitle(title);
                info.setContent(content);
                info.setInfo_type_id(info_type_id);
                info.setAuthor(author);
                info.setCreateDate(createDate);
                // 每个用户放到集合里面
                infos.add(info);
            }

            return infos;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil3.closeConn(JDBCUtil3.getCo(), JDBCUtil3.getS(), rs);
        }
        return null;
    }
}
